#include<bits/stdc++.h>
#define ll long long
#define mid ((l+r)>>1)
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N=5e5+5,M=2e6+5,inf=(1LL<<29)-1,mod=998244353;
const ll llf=2e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,ll b){if(!b)return 1;int c=qp(a,b>>1LL);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,K,b[N],m,ln;
vector<int>a[N];
int d[M],D[M],c;
ll Sum,as[M],Ans=llf;
vector<int>p[M];
#include<bits/extc++.h>
using namespace __gnu_pbds;
gp_hash_table<int,int>P;
inline void insert(int x){
	Sum-=(ll)P[x]*b[P[x]];
	P[x]++;
	Sum+=(ll)P[x]*b[P[x]];
}
inline void Main(){
	c=n=read(),d[++ln]=1;
	repn(i){
		int k=read();
		rep(j,1,k)a[i].pb(read());
		rep(j,0,k-2)d[++ln]=a[i][j+1]-a[i][j]+1,d[++ln]=a[i][j+1]-a[i][j];
	}
	sort(d+1,d+ln+1);
	rep(i,1,ln)if(d[i]!=d[i+1])D[++m]=d[i];
	K=read();
	repn(i)b[i]=read();
	repn(i){
		int k=(int)a[i].size();
		rep(j,0,k-2){
			int w=a[i][j+1]-a[i][j];
			w=lower_bound(D+1,D+m+1,w)-D;
			p[w].pb(a[i][j+1]);
		}
		Sum+=(ll)K*(a[i][k-1]-a[i][0]+D[m]),P[a[i][0]]++;
	}
	for(auto x:P)Sum+=(ll)b[x.second]*x.second;
	Ans=as[m]=Sum;
	per(i,m-1,1){
		int del=D[i+1]-D[i];
		Sum-=(ll)c*del*K;
		E(i)c++,insert(y);
		Ans=min(Ans,as[i]=Sum);
	}
	repm(i)if(as[i]==Ans)p[0].pb(D[i]);
	cout <<Ans<<' '<<(int)p[0].size()<<"\n";
	for(auto x:p[0])cout <<x<<' ';
}
signed main(){
	freopen("list.in","r",stdin);
	freopen("list.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}